package com.cn.learn.creator.domain.vo;

import com.cn.learn.creator.enumeration.ResultVOTypeEnum;
import lombok.Data;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName ResultVO
 * @Description 结果集对象
 * @Author JiJiang
 * @Date 2023/7/8 13:39
 * @Version 1.0
 */

@Data
public class ResultVO<T> {


    private Integer code;

    private String msg;

    private Integer count;

    private T data;


    public static <M> ResultVO<M> getResult(Integer code, String msg, Integer count, M data) {

        ResultVO<M> retResult = new ResultVO<>();

        retResult.setCode(code);
        retResult.setMsg(msg);
        retResult.setCount(count);
        retResult.setData(data);
        return retResult;


    }


    public static <N> ResultVO<List<N>> getResultWithCountByList(List<N> listData) {

        int count = Objects.isNull(listData) ? 0 : listData.size();
        return getResult(ResultVOTypeEnum.SUCCESS.getCode(), ResultVOTypeEnum.SUCCESS.getMsg(), count, listData);
    }

    public static <M> ResultVO<M> getResultOnlyData(M data) {
        return getResult(ResultVOTypeEnum.SUCCESS.getCode(), ResultVOTypeEnum.SUCCESS.getMsg(), 0, data);
    }
}
